using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows;
using SBPweb.Themes.Windows.Skins;

namespace SBPweb.Controls.Windows.Common
{
	internal partial class MessageBoxForm : Form
	{
		#region Private members

		private IWin32Window mOwner = null;
		private string mText = string.Empty;
		private string mCaption = string.Empty;
		private MessageBoxButtons mButtons = MessageBoxButtons.OK;
		private MessageBoxIcon mIcon = MessageBoxIcon.None;
		private MessageBoxDefaultButton mDefaultButton = MessageBoxDefaultButton.Button1;
		private MessageBoxOptions mOptions = MessageBoxOptions.DefaultDesktopOnly;
		private string mHelpFilePath = string.Empty;
		private HelpNavigator mHelpNavigator = HelpNavigator.AssociateIndex;
		private string mKeyWord = string.Empty;
		private bool mDisplayHelpButton = false;

		private Dictionary<MsgBoxButtons, string> mButtonTexts;

		#endregion

		#region Internal properties

		internal new IWin32Window Owner
		{
			get
			{
				return mOwner;
			}

			set
			{
				mOwner = value;
				base.Owner = (System.Windows.Forms.Form)mOwner;
			}
		}

		internal new string Text
		{
			set
			{
				mText = value;
				lText.Text = value;
				lText.SendToBack();
			}
		}

		internal string Caption
		{
			set
			{
				mCaption = value;
				base.Text = mCaption;
			}
		}

		internal MessageBoxButtons Buttons
		{
			set
			{
				mButtons = value;
				SetButtons(mButtons);
			}
		}

		internal new MessageBoxIcon Icon
		{
			set
			{
				mIcon = value;
				SetIcon(mIcon);
			}
		}

		internal MessageBoxDefaultButton DefaultButton
		{
			set
			{
				mDefaultButton = value;
				SetDefaultButton(mDefaultButton);
			}
		}

		internal MessageBoxOptions Options
		{
			set
			{
				mOptions = value;
				SetOptions(mOptions);
			}
		}

		internal string HelpFilePath
		{
			set
			{
				mHelpFilePath = value;
			}
		}

		internal HelpNavigator HelpNavigator
		{
			set
			{
				mHelpNavigator = value;
			}
		}

		internal string KeyWord
		{
			set
			{
				mKeyWord = value;
			}
		}

		internal bool DisplayHelpButton
		{
			set
			{
				mDisplayHelpButton = value;
			}
		}

		#endregion



		private MessageBoxButtons mMessageBoxButtons = MessageBoxButtons.OK;

		#region Constructor logic

		internal MessageBoxForm(Dictionary<MsgBoxButtons, string> buttonTexts)
			: base()
		{
			SetStyle();
			InitializeComponent();
			mButtonTexts = buttonTexts;
		}

		internal MessageBoxForm(Skin skin, Dictionary<MsgBoxButtons, string> buttonTexts)
			: base(skin)
		{
			SetStyle();
			InitializeComponent();
			mButtonTexts = buttonTexts;
		}

		internal void SetButtons(MessageBoxButtons buttons)
		{
			mMessageBoxButtons = buttons;

			this.bAbort.Visible = false;
			this.bCancel.Visible = false;
			this.bIgnore.Visible = false;
			this.bNo.Visible = false;
			this.bOK.Visible = false;
			this.bRetry.Visible = false;
			this.bYes.Visible = false;

			this.bAbort.Text = mButtonTexts[MsgBoxButtons.Abort];
			this.bCancel.Text = mButtonTexts[MsgBoxButtons.Cancel];
			this.bIgnore.Text = mButtonTexts[MsgBoxButtons.Ignore];
			this.bNo.Text = mButtonTexts[MsgBoxButtons.No];
			this.bOK.Text = mButtonTexts[MsgBoxButtons.OK];
			this.bRetry.Text = mButtonTexts[MsgBoxButtons.Retry];
			this.bYes.Text = mButtonTexts[MsgBoxButtons.Yes];

			bool ok = false;
			bool yes = false;
			bool retry = false;
			bool ignore = false;
			bool abort = false;
			bool no = false;
			bool cancel = false;
			int counter = 0;

			switch (buttons)
			{
				case MessageBoxButtons.AbortRetryIgnore:
					abort = true;
					retry = true;
					ignore = true;
					counter = 3;
					break;

				case MessageBoxButtons.OKCancel:
					ok = true;
					cancel = true;
					counter = 2;
					break;

				case MessageBoxButtons.RetryCancel:
					retry = true;
					cancel = true;
					counter = 2;
					break;

				case MessageBoxButtons.YesNo:
					yes = true;
					no = true;
					counter = 2;
					break;

				case MessageBoxButtons.YesNoCancel:
					yes = true;
					no = true;
					cancel = true;
					counter = 3;
					break;

				default:
				case MessageBoxButtons.OK:
					ok = true;
					counter = 1;
					break;
			}

			int buttonHeight = 23;
			int buttonWidth = 70;
			int buttonSpace = 6;
			int width = counter * buttonWidth + (counter - 1) * buttonSpace;
			int posX = (this.Size.Width / 2) - (width / 2);
			int posY = this.bOK.Location.Y;

			if (bOK.Visible = ok) 
			{
				bOK.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bOK.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 

			}
			if (bYes.Visible = yes) 
			{
				bYes.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bYes.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 
			}
			if (bRetry.Visible = retry) 
			{
				bRetry.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bRetry.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 
			}
			if (bIgnore.Visible = ignore) 
			{
				bIgnore.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bIgnore.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 
			}
			if (bAbort.Visible = abort) 
			{
				bAbort.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bAbort.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 
			}
			if (bNo.Visible = no) 
			{
				bNo.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bNo.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 
			}
			if (bCancel.Visible = cancel) 
			{
				bCancel.Size = new System.Drawing.Size(buttonWidth, buttonHeight);
				bCancel.Location = new Point(posX, posY); 
				posX += buttonWidth + buttonSpace; 
			}
	}

		internal void SetDefaultButton(MessageBoxDefaultButton defaultButton)
		{
			switch (defaultButton)
			{
				case MessageBoxDefaultButton.Button1:
					switch (mMessageBoxButtons)
					{
						case MessageBoxButtons.AbortRetryIgnore:
							this.bAbort.Focus();
							break;
						case MessageBoxButtons.OKCancel:
							this.bOK.Focus();
							break;
						case MessageBoxButtons.RetryCancel:
							this.bRetry.Focus();
							break;
						case MessageBoxButtons.YesNo:
							this.bYes.Focus();
							break;
						case MessageBoxButtons.YesNoCancel:
							this.bYes.Focus();
							break;
						case MessageBoxButtons.OK:
							this.bOK.Focus();
							break;
					}
					break;
				case MessageBoxDefaultButton.Button2:
					switch (mMessageBoxButtons)
					{
						case MessageBoxButtons.AbortRetryIgnore:
							this.bRetry.Focus();
							break;
						case MessageBoxButtons.OKCancel:
							this.bCancel.Focus();
							break;
						case MessageBoxButtons.RetryCancel:
							this.bCancel.Focus();
							break;
						case MessageBoxButtons.YesNo:
							this.bNo.Focus();
							break;
						case MessageBoxButtons.YesNoCancel:
							this.bNo.Focus();
							break;
					}
					break;
				case MessageBoxDefaultButton.Button3:
					switch (mMessageBoxButtons)
					{
						case MessageBoxButtons.AbortRetryIgnore:
							this.bIgnore.Focus();
							break;
						case MessageBoxButtons.YesNoCancel:
							this.bCancel.Focus();
							break;
					}
					break;
			}
		}

		internal void SetIcon(MessageBoxIcon icon)
		{
			switch (icon)
			{
				default:
				case MessageBoxIcon.None:
					pbImage.Image = null;
					pbImage.Visible = false;
					pText.Location = new Point(pText.Location.X-35, pText.Location.Y);
					break;

				case MessageBoxIcon.Asterisk:
//				case MessageBoxIcon.Information:
					pbImage.Image = global::SBPweb.Controls.Windows.Properties.Resources.Asterisk_32x32;
					break;

				case MessageBoxIcon.Error:
//				case MessageBoxIcon.Hand:
//				case MessageBoxIcon.Stop:
					pbImage.Image = global::SBPweb.Controls.Windows.Properties.Resources.Error_32x32;
					break;

				case MessageBoxIcon.Exclamation:
//				case MessageBoxIcon.Warning:
					pbImage.Image = global::SBPweb.Controls.Windows.Properties.Resources.Warning_32x32;
					break;

				case MessageBoxIcon.Question:
					pbImage.Image = global::SBPweb.Controls.Windows.Properties.Resources.Question_32x32;
					break;

			}

			mIcon = icon;
		}

		internal void SetOptions(MessageBoxOptions options)
		{
			switch (options)
			{
				default:
				case MessageBoxOptions.DefaultDesktopOnly:
					break;
			}
		}

		private void SetStyle()
		{
			//base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
			//base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			//base.SetStyle(ControlStyles.ResizeRedraw, true);
			//base.SetStyle(ControlStyles.UserPaint, false);
			//base.SetStyle(ControlStyles.AllPaintingInWmPaint, false);
		}

		#endregion

		internal void SetControlSizes()
		{
			if (pText.HorizontalScroll.Visible) pText.Height += 20;
			if (pText.VerticalScroll.Visible) pText.Width += 20;

			int addX = 72;
			if (mIcon == MessageBoxIcon.None)
			{
				addX -= 24;
				if (Skin == null) addX -= 4;
			}

			int addY = 78;
			this.Width = pText.Size.Width + addX;
			this.Height = pText.Size.Height + addY;

			int add = 20;
			if (Skin != null) add += 20;
			bOK.Location = new Point(bOK.Location.X, pText.Size.Height + add);
			bYes.Location = new Point(bYes.Location.X, pText.Size.Height + add);
			bRetry.Location = new Point(bRetry.Location.X, pText.Size.Height + add);
			bIgnore.Location = new Point(bIgnore.Location.X, pText.Size.Height + add);
			bAbort.Location = new Point(bAbort.Location.X, pText.Size.Height + add);
			bNo.Location = new Point(bNo.Location.X, pText.Size.Height + add);
			bCancel.Location = new Point(bCancel.Location.X, pText.Size.Height + add);
		}

	}
}

